// pages/question/question.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    arrays: [],
    currentAnswer: [],
    currentQuestion: "",
    finalAllQuestion: [],
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //console.log(that.data.arrays)
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
    
        })
      }
    })
  },
  onShow: function () {
    var that = this;
    for (var i = 0; i < wx.getStorageSync("userCurrentQuestions").length; i++) {
      that.data.arrays.push(i + 1);
    }
    this.setData({
      arrays: that.data.arrays,
      questionNums: wx.getStorageSync("userCurrentQuestions").length,
    })

    var allQuestions = [];
    var _answers=[];
    var aa = Math.floor(Math.random() * 24);
    var userCurrentQuestions = app.globalData.userCurrentQuestions
    for (var i = 0; i < userCurrentQuestions.length; i++) {
      allQuestions.push({
        id: userCurrentQuestions[i].id,
        answer1: userCurrentQuestions[i].answer1,
        answer2: userCurrentQuestions[i].answer2,
        answer3: userCurrentQuestions[i].answer3,
        answer4: userCurrentQuestions[i].answer4,
        question: userCurrentQuestions[i].question,
        image: userCurrentQuestions[i].image
      })
    }   
      if (allQuestions[0].answer1!=''){
        _answers.push(allQuestions[0].answer1)
      }
      if (allQuestions[0].answer2) {
        _answers.push(allQuestions[0].answer2)
      }
      if (allQuestions[0].answer3) {
        _answers.push(allQuestions[0].answer3)
      }
      if (allQuestions[0].answer4) {
        _answers.push(allQuestions[0].answer4)
      }
    that.setData({
      currentQuestion: allQuestions[0],
      currentAnswer: _answers,
      allQuestions: allQuestions
    })
  },
  changeQuetion: function () {
    var that=this;
    var count = this.data.allQuestions.length;
    var _answers = [];
    var _num = Math.floor(Math.random() * (count-1));
    console.log(_num)
    if (that.data.allQuestions[_num].answer1 != '') {
      _answers.push(that.data.allQuestions[_num].answer1)
    }
    if (that.data.allQuestions[_num].answer2) {
      _answers.push(that.data.allQuestions[_num].answer2)
    }
    if (that.data.allQuestions[_num].answer3) {
      _answers.push(that.data.allQuestions[_num ].answer3)
    }
    if (that.data.allQuestions[_num].answer4) {
      _answers.push(that.data.allQuestions[_num ].answer4)
    }
    that.setData({
      currentQuestion: that.data.allQuestions[_num],
      currentAnswer: _answers,
    })
  },
  setReallyAnswer: function (e) {
    var that=this;
    var _answers = [];
    that.data.allQuestions.splice(0, 1);
    that.data.finalAllQuestion.push({
      id: that.data.currentQuestion.id,
      answerId: e.target.id,
      answerText:e.target.dataset.name
    })
    that.data.current++;
    if (that.data.current == (that.data.questionNums+1)) {
      wx.setStorageSync("userSetQuestion", that.data.finalAllQuestion)
      wx.redirectTo({
         url: '../../pages/setMoney/setMoney',
       })
       return;
    }
    if (that.data.allQuestions[0].answer1 != '') {
      _answers.push(that.data.allQuestions[0].answer1)
    }
    if (that.data.allQuestions[0].answer2) {
      _answers.push(that.data.allQuestions[0].answer2)
    }
    if (that.data.allQuestions[0].answer3) {
      _answers.push(that.data.allQuestions[0].answer3)
    }
    if (that.data.allQuestions[0].answer4) {
      _answers.push(that.data.allQuestions[0].answer4)
    }
    that.setData({
      currentQuestion: that.data.allQuestions[0],
      currentAnswer: _answers,
      allQuestions: that.data.allQuestions,
      current: that.data.current
    })
  }
})